package br.edu.ufcg.dsc.psoo.mymoney.view.action;

import java.util.List;

import org.mentawai.core.BaseAction;
import org.mentawai.filter.AuthenticationFree;

import br.edu.ufcg.dsc.psoo.mymoney.controller.AccountController;
import br.edu.ufcg.dsc.psoo.mymoney.controller.GroupController;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.AccountNotFoundException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.GroupNotFoundException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.InvalidGroupException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.implementation.AccountControllerImpl;
import br.edu.ufcg.dsc.psoo.mymoney.controller.implementation.GroupControllerImpl;
import br.edu.ufcg.dsc.psoo.mymoney.model.bean.Account;
import br.edu.ufcg.dsc.psoo.mymoney.model.bean.Group;
import br.edu.ufcg.dsc.psoo.mymoney.model.bean.User;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.AccountDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.GroupDAO;

public class GroupAction extends BaseAction implements AuthenticationFree {

	private GroupController groupController;
	private AccountController accountController;
	private AccountDAO accountDAO;
	private GroupDAO groupDAO;

	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public void setAccountDAO(AccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}

	public void initiate() {
		groupController = new GroupControllerImpl(groupDAO, accountDAO);
		accountController = new AccountControllerImpl(accountDAO);
	}

	public boolean bypassAuthentication(String innerAction) {

		// if (innerAction != null && innerAction.equals("add")) return true;

		return true;// @FIXME sempre nao autenticar
	}

	public void setLists() {

		Long userId = ((User) getUserSession()).getId();

		List<Group> list = groupController.listAllFromUser(userId);

		output.setValue("groupList", list);

		List<Account> accList = accountController.listAll(userId);

		output.setValue("accountList", accList);
	}

	public String delete(Long id) {
		initiate();
		if (!isPost()) {
			Long userId = ((User) getUserSession()).getId();

			try {
				groupController.deleteFromUser(userId, id);
			} catch (InvalidGroupException e) {
				addMessage(e.getMessage());
				return ERROR;
			} catch (GroupNotFoundException e) {
				addMessage(e.getMessage());
				return ERROR;
			}

			addMessage("Grupo removido com sucesso!");

			setLists();

			return SHOW;

		} else {
			return ERROR;
		}
	}
		
	public String update() {
		initiate();
		if (!isPost()) {
			
			Long id = Long.parseLong(input.getString("id"));
			
			// display user for update...

			Group group = groupController.loadById(id);
			setLists();
			output.setValue("group", group);

			return SHOW;

		} else {

			Group group = new Group();
			Long id = Long.parseLong(input.getString("id"));
			String accName = input.getString("account");
			String name = input.getString("name");
			
			Account account = accountController.findByName(accName);
			
			group.setId(id);
			group.setName(name);
			group.setAccount(account);
			
			User loggedUser = (User) getUserSession();
			
			account.setUser(loggedUser);
			Group newGroup;
			try {
				newGroup = groupController.update(group);
			} catch (InvalidGroupException e) {
				setLists();
				addError("defaultError", e.getMessage());
				return SHOW;
			}
			
			output.setValue("account", newGroup.getAccount().getName());

			addMessage("Informacoes alteradas com sucesso!");
			
			return UPDATED;
		}
	}

	public String add() {
		initiate();
		if (!isPost()) {

			setLists();

			return SHOW;

		} else {
			// recebe objeto montado do Input
			Group group = input.getObject(Group.class);
			String accountName = input.getString("account");
			Account acc = new Account();
			acc.setName(accountName);

			group.setAccount(acc);

			try {
				groupController.create(group);
			} catch (InvalidGroupException e) {
				setLists();
				addMessage(e.getMessage());
				return ERROR;
			} catch (AccountNotFoundException e) {
				setLists();
				addMessage(e.getMessage());
				return ERROR;
			}

			addMessage("Grupo cadastrado com sucesso!");

			setLists();

			return SHOW;
		}
	}
}